const { router } = require('../../koa/koa-main')
const { getAllBooks, getBooksSize } = require('../../services/book')

router.get('/getAllBooks', async (ctx, next) => {
  const { data } = await getAllBooks(ctx.request.query)
  const result = await getBooksSize(ctx.request.query)
  let length = null
  for (let i = 0; i < result.data.collections.length; i++) {
    if (result.data.collections[i].name === 'books') {
      length = result.data.collections[i].count
      break
    }
  }
  data.booksLength = length
  ctx.response.body = data
  await next()
})
